package com.yesep.learn.hik.sdk;

import com.sun.jna.NativeLong;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.NativeLongByReference;

import javax.websocket.Session;

/**
 * @program: learn
 * @description: 回放回调
 * @author:
 * @create: 2019-09-11 10:35
 **/
public class FPlayDataCallBack implements HCNetSDK.FPlayDataCallBack {
    private NativeLongByReference m_lPort = new NativeLongByReference(new NativeLong(-1));

    private DecCBack dec;

    public FPlayDataCallBack(Session session) {
        dec = new DecCBack(session);
    }

    @Override
    public void invoke(NativeLong lPlayHandle, int dwDataType, ByteByReference pBuffer, int dwBufSize, int dwUser) {
        System.out.println("回放回调正在执行...");
        switch (dwDataType) {
            case HCNetSDK.NET_DVR_SYSHEAD:  //系统头
                if (!SDKUtils.getPlayControl().PlayM4_GetPort(m_lPort)) {//获取播放库未使用的通道号
                    break;
                }

                if (dwBufSize > 0) {
                    //设置实时流播放模式
                    if (!SDKUtils.getPlayControl().PlayM4_SetStreamOpenMode(m_lPort.getValue(), PlayCtrl.STREAME_REALTIME)) {
                        break;
                    }
                    //打开流接口
                    if (!SDKUtils.getPlayControl().PlayM4_OpenStream(m_lPort.getValue(), pBuffer, dwBufSize, 1024 * 1024)) {
                        break;
                    }
                    //设置解码器
                    if (!SDKUtils.getPlayControl().PlayM4_SetDecCallBack(m_lPort.getValue(), dec)) {
                        break;
                    }
                    //播放开始
                    if (!SDKUtils.getPlayControl().PlayM4_Play(m_lPort.getValue(), null)) {
                        break;
                    }
                }
            case HCNetSDK.NET_DVR_STREAMDATA:   //流数据
                if (dwBufSize > 0) {
                    try {
                        if (m_lPort.getValue().intValue() != -1 && dec != null && dec.getSession() != null && dec.getSession().isOpen()) {
                            try {
                                if (!SDKUtils.getPlayControl().PlayM4_InputData(m_lPort.getValue(), pBuffer, dwBufSize)) {
                                    break;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                dec.getSession().close();
                                System.out.println("关闭连接");
                                return;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case HCNetSDK.NET_DVR_CHANGE_FORWARD:  //码流改为正放

                break;
            case HCNetSDK.NET_DVR_CHANGE_REVERSE:  //码流改为倒放

                break;
        }
    }
}
